package blog.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.CommentService;

import blog.entity.Article;
import blog.entity.Comment;
import blog.mapper.ArticleMapper;
import blog.mapper.CommentMapper;

@Service
public class CommentServiceImpl implements CommentService{
	//持久层对象
	@Autowired
	private  CommentMapper commentMapper;
	@Autowired
	private ArticleMapper articleMapper;
	
	/**
	 * 	最近的评论
	 * @param 查询条数
	 * @return 最近的评论*条数
	 */
	public List<Comment> listRecentComment(Integer n) {
		//查询最近的评论*条数
		List <Comment> commentList=commentMapper.listRecentComment(n);
		
		//根据 被评论的文章的ID 查询 被评论的文章 并放在每一条被评论的文章信息中
		for(Comment c:commentList) {	
			Article article =articleMapper.getArticleById(c.getCommentArticleId());
			c.setArticle(article);
		}
		return  commentList;
	}

	@Override
	public PageInfo<Comment> commentList(Integer pageNum, Integer pageSize) {
		//分页
		PageHelper.startPage(pageNum, pageSize);
		//查询
		List<Comment> commentList = commentMapper.commentList();
		//获取每条评论对应的被评论的文章
		for(Comment c:commentList) {	
			Article article =articleMapper.getArticleById(c.getCommentArticleId());
			c.setArticle(article);
		}
		return new PageInfo<Comment>(commentList);
	}

	
}
